/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.proportional;

import control.CVisualizzaMessaggio;
import customGuiControl.RoundedPanel2;
import entity.xml.EMessaggio;
import foundation.FMessaggi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.netbeans.lib.awtextra.AbsoluteLayout;
import org.w3c.dom.events.MouseEvent;
import presentation.messaggioPanel;

/**
 *
 * @author Quirino
 */
public class PVisualizzaMessaggio implements MouseListener{
    
    private static PVisualizzaMessaggio instance= null;
    private JPanel panel= null;
    private RoundedPanel2 menuSotto= null;
    private CVisualizzaMessaggio cVisMess= null;
    
    
    private PVisualizzaMessaggio(){
        
        this.panel= new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.panel.setOpaque(false);
        this.cVisMess= CVisualizzaMessaggio.getInstance();
        
    }
    
    public static PVisualizzaMessaggio getInstance(){
        
        if(instance==null) instance= new PVisualizzaMessaggio();
        return instance;
        
    }
    
    public void markAsRead(messaggioPanel messagePanel, EMessaggio mess){
        
        this.cVisMess.changeStatoMsg(mess);
        
    }
    
    public void visualizzaMessaggio(messaggioPanel messagePanel){
        
        this.panel.removeAll();
        
        ArrayList<EMessaggio> conversazione= new ArrayList<EMessaggio>();
        conversazione= messagePanel.getConversazione();
        
        for(int l=0; l<conversazione.size(); l++){
            if(conversazione.get(l).getStato()==0) this.markAsRead(messagePanel, conversazione.get(l));
        }
        
        EMessaggio firstMessage= conversazione.get(0);
        
        Dimension d = this.cVisMess.getDimensionFoglio(2, 1.1);
        Dimension dimBacheca= this.cVisMess.getDimensionBacheca();
        
        int left= ((int)dimBacheca.width/2)-((int)(d.width/2));
        int top= ((int)dimBacheca.height/2)-((int)(d.height/2));
        
        JLayeredPane jlp= new JLayeredPane();
        jlp.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()));
        jlp.setBackground(Color.red);
        jlp.setBounds(left, top-50, (int)d.getWidth(), (int)d.getHeight());
        jlp.setOpaque(false);
        
        ImageIcon imgIcon= Utility.getResizedImageIcon("foglio.png", (int)d.getWidth(), (int)d.getHeight()-10);
        JLabel foglioContainer= new JLabel(imgIcon);
        foglioContainer.setBounds(0, 0, (int)d.getWidth(), (int)d.getHeight()-10);
        jlp.add(foglioContainer, new Integer(0));
        
        JLabel labelDestinatari= new JLabel();
        Font fontHeaderFoglio= new Font("Droid Sans", Font.BOLD, (int)d.getWidth()/24);
        labelDestinatari.setFont(fontHeaderFoglio);
        String str= "<html><b><u>Messaggio verso:</u><b> ";
        for(int i=0; i<firstMessage.getDestinatario().size(); i++){
            
            if(i!=0) str+= ", " ;
            str+= firstMessage.getDestinatario().get(i).getNickname();
            
        }
        str+="<br><b><u>Messaggio da:</b></u> " + firstMessage.getMittente().getNickname();
        str+="</html>";
        labelDestinatari.setText(str);
        labelDestinatari.setBounds((int)d.getWidth()/8, 0, (int)d.getWidth()-(int)d.getWidth()/10, (int)d.getHeight()/5);
        jlp.add(labelDestinatari, new Integer(1));
        
        JTextArea txtAreaMessaggio= new JTextArea(firstMessage.getContenuto());
        Font fontContenutoFoglio= new Font("Droid Sans", Font.PLAIN, (int)d.getWidth()/30);
        txtAreaMessaggio.setBounds((int)d.getWidth()/8, (int)d.getHeight()/5, (int)d.getWidth()-(int)d.getWidth()/6, (int)d.getHeight()-(int)d.getHeight()/10);
        txtAreaMessaggio.setOpaque(false);
        txtAreaMessaggio.setEditable(false);
        txtAreaMessaggio.setFont(fontContenutoFoglio);
        txtAreaMessaggio.setLineWrap(true);
        txtAreaMessaggio.setWrapStyleWord(true);
        jlp.add(txtAreaMessaggio, new Integer(2));
        
        this.panel.add(jlp, JLayeredPane.PALETTE_LAYER);
        
    }
    
    public JPanel getPanel(){
        
        return this.panel;
        
    }
    
    public RoundedPanel2 getMenuSotto(gui instance){
        
        if(this.menuSotto==null) this.menuSotto= this.refreshMenuSotto(instance);
        return this.menuSotto;
        
    }
    
    public RoundedPanel2 refreshMenuSotto(gui instance){
        
       /* RoundedPanel2 rp= new RoundedPanel2(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addRispondiMessaggioButton(gbc, rp, this, instance);
        
        
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=2;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addInoltraMessaggioButton(gbc, rp, this, instance);
        
        gbc.gridx=3;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addAscoltaMessaggioButton(gbc, rp, this, instance);
        
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=6;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addEliminaMessaggioButton(gbc, rp, this, instance);
        
        return rp;
        
    }

    public void mouseClicked(java.awt.event.MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(java.awt.event.MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(java.awt.event.MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(java.awt.event.MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(java.awt.event.MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
